/*
 *  RESTful DDS is a prototyping effort to demonstrate how DDS could be combined
 *  with HTTP clients, extending the realm of pub/sub distribution to true wide
 *  area networking.
 *
 *  Copyright (C) 2009 PrismTech Ltd.
 *  reinier.torenbeek@gmail.com
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License Version 3 dated 29 June 2007, as published by the
 *  Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with restful-dds; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.opensplice.restful.service;

import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.ResourceException;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

import com.google.gson.Gson;

import DDS.DomainParticipant;
import DDS.DomainParticipantFactory;
import DDS.DomainParticipantQosHolder;

public class ParticipantResource extends Resource {
    
    DDSParticipantProxy participant;
    String participantName;
    
    public ParticipantResource(Context context, Request request, Response response) {
        super(context, request, response);
        participantName = (String) request.getAttributes().get("participantname");

        DDSRootProxy rootProxy = ((RESTfulDDSApplication)getApplication()).getRootProxy();
        participant = rootProxy.lookupParticipant(participantName);
        if (participant != null) {
            /* Participant found so this can be PUT or DELETED */
            setModifiable(true);
            getVariants().add(new Variant(MediaType.TEXT_PLAIN));
        } else {
            setModifiable(false);
        }
    }    

    /**
     * Handle DELETE requests.
     */
    @Override
    public void removeRepresentations() throws ResourceException {
        if (participant != null) {
            DomainParticipant domainParticipant = participant.getParticipant();
            if (domainParticipant != null) {
                domainParticipant.delete_contained_entities();
                DomainParticipantFactory dpf = DomainParticipantFactory.get_instance();
                if (dpf != null) {
                    int status = dpf.delete_participant(domainParticipant);
                    ErrorHandler.checkStatus(status, "DDS.DomainParticipantFactory.delete_participant");
                }
            }
            DDSRootProxy rootProxy = ((RESTfulDDSApplication)getApplication()).getRootProxy();
            rootProxy.deleteParticipant(participantName);
        }

        /* Tells the client that the request has been successfully fulfilled. */
        getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
    }
    
    
    @Override
    public Representation represent(Variant variant) throws ResourceException {

        Representation result = null;

        if ((variant != null)
                && variant.getMediaType().equals(MediaType.TEXT_PLAIN)) {
            DomainParticipant domainParticipant = participant.getParticipant();
            if (domainParticipant != null) {
                DomainParticipantQosHolder dpQosHolder = new DomainParticipantQosHolder();
                int status = domainParticipant.get_qos(dpQosHolder);
                ErrorHandler.checkStatus(status, "DDS.DomainParticipant.get_qos");
                Gson gson = new Gson();
                String jstring = gson.toJson(dpQosHolder);
                result = new StringRepresentation(jstring, MediaType.TEXT_PLAIN);
                dpQosHolder = null;
            }
        }
        if (result != null) {
            getResponse().setStatus(Status.SUCCESS_OK);
        } else {
            getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
        }
        return result;
        
    }
    
}

